﻿using Siasun.AFC.RtPlatfromGui.IBll.IServer;
using Siasun.AFC.RtPlatfromGui.IBll.Model;
using Siasun.AFC.RtPlatfromGui.UI.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Siasun.AFC.RtPlatfromGui.UI.BusinessHelper
{
    /// <summary>
    /// 菜单数据获取帮助类
    /// </summary>
    public class MenuDataHelper
    {
        #region 字段
        /// <summary>
        /// 临时获取使用的数据库菜单集合
        /// </summary>
        private List<MenuInfo> tempMenuInfoList = null;
        #endregion

        #region 饿汉构造
        private static MenuDataHelper instance = new MenuDataHelper();

        private MenuDataHelper()
        {

        }

        public static MenuDataHelper GetInstanse()
        {
            return instance;
        }
        #endregion

        #region 方法

        #region 私有方法
        /// <summary>
        /// 获取根节点Id
        /// </summary>
        /// <returns></returns>
        private string GetRootNodeMenuId()
        {
            MenuInfo menuInfo = tempMenuInfoList.Where(a => a.NodeType.ToLower() == "root").FirstOrDefault();
            if (!object.Equals(null, menuInfo) && !string.IsNullOrEmpty(menuInfo.Id))
            {
                tempMenuInfoList.Remove(menuInfo);
                return menuInfo.Id;
            }
            return "1001";
        }

        /// <summary>
        /// 递归获取菜单树
        /// </summary>
        /// <param name="dataId"></param>
        /// <returns></returns>
        private ObservableCollection<MenuTreeInfo> GetMenuTree(string dataId)
        {
            //获取全部子集
            List<MenuInfo> childMenuInfoList = tempMenuInfoList.Where(a => a.ParentMenuId == dataId).ToList();
            //移除已获取的子集
            childMenuInfoList.ForEach(t => tempMenuInfoList.Remove(t));

            ObservableCollection<MenuTreeInfo> menuTreeInfoList = new ObservableCollection<MenuTreeInfo>();
            MenuTreeInfo menuChildInfo = null;
            foreach (var item in childMenuInfoList)
            {
                menuChildInfo = new MenuTreeInfo();
                menuChildInfo.MenuName = item.MenuName;
                menuChildInfo.PageName = item.MenuPageName;
                menuChildInfo.IsCanUse = item.MenuIsable >= 1 ? true : false;
                menuChildInfo.MenuTag = item;
                menuChildInfo.ChildrenList = GetMenuTree(item.Id);

                menuTreeInfoList.Add(menuChildInfo);
            }

            return menuTreeInfoList;
        }

        /// <summary>
        /// 添加没有找到分支的菜单
        /// </summary>
        /// <param name="menuTreeArray"></param>
        /// <param name="menuList"></param>
        /// <returns></returns>
        private ObservableCollection<MenuTreeInfo> AddAnotherMenu(ObservableCollection<MenuTreeInfo> menuTreeArray, List<MenuInfo> menuList)
        {
            if (menuList.Count <= 0) return menuTreeArray;

            MenuTreeInfo menuChildInfo = null;
            foreach (var item in menuList)
            {
                menuChildInfo = new MenuTreeInfo();
                menuChildInfo.MenuName = item.MenuName;
                menuChildInfo.PageName = item.MenuPageName;
                menuChildInfo.IsCanUse = item.MenuIsable >= 1 ? true : false;
                menuChildInfo.MenuTag = item;
                //menuChildInfo.ChildrenList = GetMenuTree(item.Id);//补充显示的时候拿不到根元素所以，统一都不设置根节点
                menuTreeArray.Add(menuChildInfo);
            }
            return menuTreeArray;
        }
        #endregion

        #region 共有方法
        /// <summary>
        /// 获取提供使用的菜单树
        /// </summary>
        /// <returns></returns>
        public ObservableCollection<MenuTreeInfo> GetMenuTreeForUse()
        {
            //创建菜单数据工厂
            IMenuDataServer menuDataServer = ServerFactory.CreateMenuDataServer();
            //获取全部菜单数据
            this.tempMenuInfoList = menuDataServer.QueryAllMenuData();
            //获取根元素树
            ObservableCollection<MenuTreeInfo> menuTreeList = GetMenuTree(GetRootNodeMenuId());
            //延续没有根元素 的菜单
            AddAnotherMenu(menuTreeList, tempMenuInfoList);

            return menuTreeList;
        }
        #endregion 

        #endregion
    }
}
